#include <fstream>
#include <iostream>
#include <iomanip>
#include "TFile.h"
#include "TTree.h"
#include "TSystem.h"
#include "TH1.h"
#include "TH2.h"
#include "TEllipse.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TClonesArray.h"

using namespace std;

void Nphoton(){

  int ret = gSystem->Load( "../Objects/libObjectClass.so" );
  if( ret!=0 ){
    cout << "Failed to Load Library: ret = " << ret << endl;
    return;
  }

  //  TH1F *hPhotons=new TH1F("hPhotons","hPhotons",105,0,105);
  TGraph *gPhotons=new TGraph();
  int ene[11]={ 5 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 };

  for(int i=0;i<11;i++){

    TFile *fread=new TFile(Form("../outputFiles/Ar_0V_%dkeV.root",ene[i]));
    TTree *tree=(TTree*)fread->Get("tree");

    TClonesArray *JPMTa=new TClonesArray("PMT");
    TClonesArray *MPMTa=new TClonesArray("PMT");
    tree->GetBranch("JPMTb")->SetAutoDelete(kFALSE);
    tree->GetBranch("MPMTb")->SetAutoDelete(kFALSE);
    tree->SetBranchAddress("JPMTb",&JPMTa);
    tree->SetBranchAddress("MPMTb",&MPMTa);

    int nPhoton;
    tree->SetBranchAddress("nPhoton",&nPhoton);
    TH1F *hNphoton=new TH1F("hNphoton","hNphoton",100000,0,100000);

    int nev = tree->GetEntries();
    for(int iev=0;iev<nev;iev++){
      JPMTa->Clear();
      MPMTa->Clear();
      tree->GetEntry(iev);

      hNphoton->Fill(nPhoton);
    }

    //    hPhotons->Fill((double)ene[i],hNphoton->GetMean());
    gPhotons->SetPoint(i,ene[i],hNphoton->GetMean());

    hNphoton->Delete();
    JPMTa->Delete();
    MPMTa->Delete();
    fread->Delete();
  }

  TCanvas *c1=new TCanvas("c1","c1",800,600);
  c1->SetGrid();
  TH1 *frame=gPad->DrawFrame(0,0,105,1000);
  frame->SetXTitle("E_{r} (keV)");
  frame->SetYTitle("N of Photons");
  frame->Draw();
  gPhotons->SetMarkerSize(1.5);
  gPhotons->SetMarkerStyle(21);
  gPhotons->Draw("p,same");

}
